// 客户模糊查询
import React from 'react';
import { Spin, Select } from 'antd';
import { connect } from 'dva';
import HOCSelect from '@/components/AntDesign/Select';

const { Option } = Select;

@connect(({ publicdata: { custList, custFsValue }, loading }) => ({
  custList,
  custFsValue,
  loading: loading.effects['publicdata/fetchCustList'],
}))
class Customer extends React.Component {
  componentDidMount() {
    const { onRef, custFsValue } = this.props;
    if (onRef) {
      onRef(this);
    }
    // 创建时有客户信息就去查询一次
    this.search(custFsValue?.title || '');
  }

  componentWillUnmount() {
    const { dispatch } = this.props;
    dispatch({
      type: 'publicdata/updateModelData',
      payload: { custList: [] },
    });
  }

  // 获取地址
  search = value => {
    const { dispatch, attr, params = {}, isSelfSupp = false, isEsOnceCust = false } = this.props;
    if (value.trim().length) {
      if (attr) {
        dispatch({
          type: 'publicdata/fetchEsOsCustList',
          payload: { name: value.trim(), ...params, isSelfSupp },
        });
      } else if (isEsOnceCust) {
        // isEsOnceCust:员工一次性费用新增查询接口不同
        dispatch({
          type: 'publicdata/fetchEsOnceCustList',
          payload: { name: value.trim(), ...params, isSelfSupp },
        });
      } else {
        dispatch({
          type: 'publicdata/fetchCustList',
          payload: { name: value.trim(), ...params, isSelfSupp },
        });
      }
    }
  };

  // 更改当前选择的值并供给父组件用
  triggerChange(changeValue, options) {
    const { onChange, dispatch, ...rest } = this.props;
    const payload = {
      custFsValue: rest.labelInValue ? { key: options.key, label: options.title } : options,
    };
    if (onChange) {
      onChange(changeValue, options);
    }
    dispatch({
      type: 'publicdata/setCustFsValue',
      payload,
    });
  }

  render() {
    const {
      custList = [],
      loading,
      getData = () => {},
      attr,
      isShowSupp = false,
      value,
      custFsValue,
      ...rest
    } = this.props;
    let useValue;
    if (rest.labelInValue) {
      if (custFsValue.value) {
        useValue = custFsValue;
      } else {
        useValue = value;
      }
    } else if (value) {
      useValue = custFsValue.title;
    } else {
      useValue = value;
    }

    return (
      <HOCSelect
        placeholder="请输入关键字检索"
        {...rest}
        showSearch
        value={useValue}
        optionFilterProp="children"
        showArrow={false}
        onSearch={this.search}
        onSelect={(selectValue, options) => getData(selectValue, options)}
        notFoundContent={loading ? <Spin size="small" /> : '暂无数据'}
        onChange={(changeValue, options) => this.triggerChange(changeValue, options)}
      >
        {custList.map(item => (
          <Option
            key={attr ? item.mdCustId : item.custId}
            title={isShowSupp && item.suppName ? `${item.name}(${item.suppName})` : item.name}
            fsOptions={item}
          >
            {isShowSupp && item.suppName ? `${item.name}(${item.suppName})` : item.name}
          </Option>
        ))}
      </HOCSelect>
    );
  }
}
export default Customer;
